//
//  PasswordchoiceViewController.m
//  PatternLockApp
//
//  Created by Nikhil on 06/06/13.
//
//

#import "PasswordchoiceViewController.h"
#import "PasswordViewController.h"
#import "ChangePasswordViewController.h"
@interface PasswordchoiceViewController ()

@end

@implementation PasswordchoiceViewController
{
    NSArray *mpasswordChoices;
    PasswordType mPasswordType;
}
@synthesize mPatternLockViewController,mKey,mTableView,mNavigationController;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    self.navigationController.delegate=self;
    mPatternLockViewController=[DrawPatternLockViewController new];
    mKey=[[NSUserDefaults standardUserDefaults] objectForKey:kPattern];
    mpasswordChoices=[[NSArray alloc] initWithObjects:@"Pattern Password",@"Pin Code Password", nil];
    mPasswordType=[[NSUserDefaults standardUserDefaults] integerForKey:kPasswordType];
    
        // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)viewDidAppear
{
    [mTableView reloadData];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [mpasswordChoices count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *rcell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (rcell == nil) {
        rcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    rcell.textLabel.text=[mpasswordChoices objectAtIndex:[indexPath row]];
    rcell.selectionStyle=UITableViewCellSelectionStyleNone;
    mPasswordType=[[NSUserDefaults standardUserDefaults] integerForKey:kPasswordType];
    if ([indexPath row] ==mPasswordType -1) {
        rcell.accessoryType=UITableViewCellAccessoryCheckmark;
    }
    else
    {
        rcell.accessoryType=UITableViewCellAccessoryNone;
    }
    // Configure the cell...
    
    return rcell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate
-(void)pushPinPasswordcontroller
{
    PasswordViewController *tController = [[PasswordViewController alloc] initWithNibName:@"PasswordViewController" bundle:nil];
    [self.navigationController pushViewController:tController  animated:YES];
}
-(void)pushPatternPasswordController
{
    [mPatternLockViewController setTarget:self withAction:@selector(compareKey:key2:)];
    [self.navigationController pushViewController:mPatternLockViewController animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch ([indexPath row]) {
        case 0:
            if (mPasswordType==2)
            {
                [self  pushChangePinPasswordAlertView];
                
            }
            else if(mPasswordType ==0)
            {
                [self pushPatternPasswordController];
            }
            break;
            case 1:
            if (mPasswordType==1) {
                [mPatternLockViewController setTarget:self withAction:@selector(checkKey:)];
                [self.navigationController pushViewController:mPatternLockViewController  animated:YES];
                
            }
            else if(mPasswordType==0)
            {
                PasswordViewController *tController = [[PasswordViewController alloc] initWithNibName:@"PasswordViewController" bundle:nil];
               [self.navigationController pushViewController:tController  animated:YES];
  
                mPasswordType=2;
            }
            break;
        default:
            break;
    }
    
    [tableView reloadData];
    
}
-(void)pushChangePinPasswordAlertView
{
    UIAlertView *tChangePinPasswordAlertview=[[UIAlertView alloc]initWithTitle:@"Change Password to Pattern" message:@"Enter your old Pin Password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    [tChangePinPasswordAlertview setAlertViewStyle:UIAlertViewStyleSecureTextInput];
    UITextField *tOldPasswordTextField = [tChangePinPasswordAlertview textFieldAtIndex:0];
    tOldPasswordTextField.keyboardType = UIKeyboardTypeNumberPad;
    tChangePinPasswordAlertview.tag=2;
    [tChangePinPasswordAlertview show];
}
-(void)compareKey:(NSString *)key1 key2:(NSString *)key2
{
    if ([key1 isEqual:key2]) {
        self.mKey=[NSString new];
        self.mKey=key2;
        NSLog(@"------pattern %@ mkey %@ pattern1 %@",[[NSUserDefaults standardUserDefaults] objectForKey:kPattern],mKey,[[NSUserDefaults standardUserDefaults] objectForKey:kPattern1]);
        [self.navigationController popViewControllerAnimated:YES];
        if (mPasswordType==2) {
            mPasswordType--;
        }
        else
        {
        mPasswordType++;
        }
        [[NSUserDefaults standardUserDefaults] setInteger:mPasswordType forKey:kPasswordType];
    }
    
}
-(void)checkKey:(NSString *)key1
{
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"------pattern %@ mkey %@ pattern1 %@",[[NSUserDefaults standardUserDefaults] objectForKey:kPattern],mKey,[[NSUserDefaults standardUserDefaults] objectForKey:kPattern1]);
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:kPattern ] isEqual:key1]) {
        [self performSelector:@selector(pushPinPasswordcontroller) withObject:nil afterDelay:0.1];
    }else
    {
        UIAlertView *tAlertView=[[UIAlertView alloc] initWithTitle:@"Incorrect Pattern" message:@"You Have Entered wrong Password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Retry", nil];
        [tAlertView show];
    }
    
}
//- (void) goBack{
//    
//    NSArray *currentControllers = self.navigationController.viewControllers;
//    NSMutableArray *newControllers = [NSMutableArray arrayWithArray:currentControllers];
//    NSLog(@"---------------count of navigatioin controllers%d",[newControllers count]);
//    [newControllers removeLastObject];
//    [self.navigationController setViewControllers:newControllers animated:YES];
//}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ([[alertView buttonTitleAtIndex:buttonIndex] isEqual:@"Cancel"]) {
        if (alertView.tag==2||alertView.tag==3) {
            return;
        }
        else
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
            return;
    }
    if([[alertView buttonTitleAtIndex:buttonIndex] isEqual:@"Retry"]&&alertView.tag==3)
    {
        [self pushChangePinPasswordAlertView];
        return;
    }
    if (alertView.tag==2) {
    if([[alertView textFieldAtIndex:0].text isEqual:[[NSUserDefaults standardUserDefaults] objectForKey:kPasswordText] ])
    {
        NSLog(@"-------pin password %@",[[NSUserDefaults standardUserDefaults] objectForKey:kPasswordText]);
        [[NSUserDefaults standardUserDefaults]
         removeObjectForKey:kPattern];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kPattern1];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:kPattern2];
        [self pushPatternPasswordController];
    }
    else
    {
        UIAlertView *wrongPasswordAlertView=[[UIAlertView alloc]initWithTitle:@"Wrong Password" message:@"You have enterd wrong Password" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"Retry", nil];
        wrongPasswordAlertView.tag=3;
        [wrongPasswordAlertView show];
    }
    }
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
{
    if ([[viewController class] isEqual:[PasswordchoiceViewController class]]) {
        [self.mTableView reloadData];
        
    }
}
@end
